<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">取快递</block>
		</cu-custom>
		<view class="cu-form-group">
			<view class="title">取件短信(取件)</view>
			<input placeholder="请输入取件短信(取件)" v-model="content" name="input"></input>
			<button class='cu-btn bg-green shadow' @tap="getButton">取件</button>
		</view>
		<view class="padding-xs flex align-center">
			<view class="flex-sub text-center">
				<view class="solid-bottom text-xs padding">
					<text class="text-gray">
						亲爱的顾客：
						感谢您选择我们的取件服务！为了给您带来更多便利，现提供两种取件方式供您选择：
						<br />
						<span class="text-red">①便捷取件：</span>
						观看一段激励视频，随后系统将自动识别您取件短信中的内容，精准提取取件码，并为您打开对应柜门。轻松几步，快速取件，节省您的时间与精力。
						<br />
						<span class="text-red">②自行输入：</span>
						若您不想观看视频，也可直接手动输入短信中的取件码完成取件。不过，取件码通常随机且较长，输入时还请您仔细核对，避免出错。
						观看激励视频不仅能让您便捷取件，还能助力我们持续优化服务。期待您的选择，祝您取件顺利！
					</text>
				</view>
			</view>
		</view>
		<view class="cu-bar bg-white margin-top">
			<view class="action">
				<text class="cuIcon-title text-pink"></text> 柜门状态
			</view>
		</view>
		<view class="padding cell-status">
			<view class="cu-avatar xl round margin-left bg-red">{{status}}</view>
		</view>
	</view>
</template>

<script>
	let videoAd = null;
	import {
		mapGetters,
		mapActions
	} from "vuex";
	export default {
		name: 'personage',
		data() {
			return {
				content: '',
				enableAdvertising: false,
				code: '',
				status: '关',
			}
		},
		computed: {
			...mapGetters(['isLogin'])
		},
		onLoad() {
			if (wx.createRewardedVideoAd) {
				videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-4fd93ede54d8a3ad'
				})
				videoAd.onLoad(() => {})
				videoAd.onError((err) => {
					console.error('激励视频光告加载失败----->', err);
				})
				videoAd.onClose((res) => {
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						//开启柜门----->
						//todo
						this.openCell();
					} else {
						// 播放中途退出，不下发游戏奖励
						console.log('播放中途退出，不下发游戏奖励-------->');
					}
				})
			}
		},
		created() {
		},
		methods: {
			...mapActions(['loginAction', 'userInfoAction', 'isLoginAction']),
			getButton() {
				this.getEnableAdvertising().then((res) => {
					this.enableAdvertising = res;
					let oldContent = this.content;
					if (this.content == '') {
						uni.showModal({
							title: '提示',
							content: '请输入正确的短信取件码!',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									that.getCode2();
								} else if (res.cancel) {
									return;
								}
							}
						});
						return;
					}
					const regex = /MY-\d+-\d+/;
					const result = this.content.match(regex);
					if (result) {
						let code = result[0];
						this.code = code;
						//判断是否需要看广告
						if (res) {
							console.log('开启广告---->');
							this.openAdvertising();
						} else {
							console.log('关闭广告---->');
							this.openCell();
						}
					} else {
						uni.showModal({
							title: '提示',
							content: '请输入正确的短信取件码!',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									that.getCode2();
								} else if (res.cancel) {
									return;
								}
							}
						});
						return;
					}

				});
			},
			openCell() {
				this.$http.get('/santu/tbCell/open', {
					params: {
						code: this.code,
						codeStr: this.content
					}
				}).then((res) => {
					let message = res.data.result;
					if (res.data.code == 200) {
						this.status = '开';
						uni.showToast({
							title: message
						});
						return;
					} else {
						this.status = '关';
					}
				})
			},
			openAdvertising() {
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.error('激励视频 广告显示失败------>', err);
							})
					})
				}
			},
			async getEnableAdvertising() {
				return new Promise((resolve, reject) => {
					this.$http.get('/santu/tbAdvertising/enablejilishipinAdvertising').then((res) => {
						let result = res.data.result;
						if (result == 'success!') {
							this.enableAdvertising = true;
							resolve(true);
							console.log('开启广告----->');
						} else if (result == 'failure!') {
							this.enableAdvertising = false;
							resolve(false);
							console.log('关闭广告----->');
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.cell-status {
		display: flex;
		justify-content: center;
	}
</style>